//Master Implementation

#include "master.h"

#include <iostream>
using namespace std;

master::master(){
                 //Nothing here
}

master::master(sync *sync_object, control *control_object, sweeps *sweeps_object, Deli *deli_object, shop *shop_object, checkout *checkout_object){
                sync_obj = sync_object;
                control_obj = control_object;
                sweeps_obj = sweeps_object;
                deli_obj = deli_object;
                shop_obj = shop_object;
                checkout_obj = checkout_object;
}

void master::run_sim(){
                  while(((*control_obj).peek((*sync_obj))).getArrivalTime() == (*sync_obj).return_time() && ((*control_obj).peek((*sync_obj))).isInSweepstakes() && ((*control_obj).peek((*sync_obj))).getFirstName() != "")
                                                              (*sweeps_obj).add2queue((*control_obj).empty2store((*sync_obj)));
                  cout << "Control Object Emptied to Sweeps." << endl;
                  while(((*control_obj).peek((*sync_obj))).getArrivalTime() == (*sync_obj).return_time() && ((*control_obj).peek((*sync_obj))).isInSweepstakes() && ((*control_obj).peek((*sync_obj))).getFirstName() != "")
                                                              (*deli_obj).insert((*control_obj).empty2store((*sync_obj)));
                  cout << "Control Object Emptied to Deli." << endl;
                  if(((*sweeps_obj).peek()).getArrivalTime() == (*sync_obj).return_time() && ((*sweeps_obj).peek()).isInDeli() && ((*sweeps_obj).peek()).getFirstName() != "")
                                                              (*deli_obj).insert((*control_obj).empty2store((*sync_obj)));
                  cout << "Sweeps Object Emptied to Deli." << endl;
                  
                  (*shop_obj).start_shopping(); cout << "Control, Sweeps, and Deli Object Emptied to Shop." << endl;
                  (*checkout_obj).start_checkout(); cout << "Shop Object Emptied to Checkout Lines." << endl;
                  (*checkout_obj).exit_store(); cout << "Checkout Lines Emptied." << endl;
                  update_sync(); cout << "Sync Time Updated." << endl;
}

void master::update_sync(){
     cout << "Update started." << endl;
     int lowest = 0;
     int i = 0;
     sync temp_sync; cout << "Temp Sync Made." << endl;
     temp_sync.sync_time(0); cout << "Temp Sync Set to 0." << endl;
    while(((*control_obj).peek(temp_sync)).getArrivalTime() == -1){
                 i++;
                 //cout << i << endl;
                 temp_sync.sync_time(i);
    }
    //lowest = ((*control_obj).peek(temp_sync)).getArrivalTime();
                       
     lowest = ((*control_obj).peek(temp_sync)).getArrivalTime();
     if(lowest > ((*deli_obj).peek()).getActionTime() && ((*deli_obj).peek()).getActionTime() > 0)
               lowest = ((*deli_obj).peek()).getActionTime();
     if(lowest > ((*sweeps_obj).peek()).getActionTime() && ((*sweeps_obj).peek()).getActionTime() > 0)
               lowest = ((*sweeps_obj).peek()).getActionTime();
     if(lowest > ((*shop_obj).peek()).getActionTime() && ((*shop_obj).peek()).getActionTime() > 0)
               lowest = ((*shop_obj).peek()).getActionTime();
     if(lowest > ((*checkout_obj).peek()).getActionTime() && ((*checkout_obj).peek()).getActionTime() > 0)
               lowest = ((*checkout_obj).peek()).getActionTime();
               
     //cout << lowest << endl;
     
     
     (*sync_obj).sync_time(lowest);
}

void master::for_the_win(){
     (*sweeps_obj).show_stats(); cout << endl << endl;
     (*deli_obj).show_stats(); cout << endl << endl;
     (*shop_obj).show_stats(); cout << endl << endl;
     (*checkout_obj).show_stats(); cout << endl << endl;
     
     cout << "Of " << (*sweeps_obj).sweeps_size() << " entries, the lucky winner of the sweepstakes is " << (*sweeps_obj).draw_winner() << "! Congratulations." << endl;
     
}
     
